package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.BrokerRewardsRecordForm;
import com.yxinsur.imc.business.vo.BrokerRewardsRecordVo;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcBrokerRewardsRecordMapper;
import com.yxinsur.imc.business.domain.ImcBrokerRewardsRecord;
import com.yxinsur.imc.business.service.IImcBrokerRewardsRecordService;

/**
 * 代理人奖惩记录Service业务层处理
 * 
 * @author imc
 * @date 2021-07-13
 */
@Service
public class ImcBrokerRewardsRecordServiceImpl extends ServiceImpl<ImcBrokerRewardsRecordMapper, ImcBrokerRewardsRecord> implements IImcBrokerRewardsRecordService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcBrokerRewardsRecordServiceImpl.class);

    @Autowired
    private ImcBrokerRewardsRecordMapper imcBrokerRewardsRecordMapper;
    /**
     * 查询代理人奖惩记录列表
     * 
     * @param brokerRewardsRecordForm 代理人奖惩记录
     * @return 代理人奖惩记录
     */
    @Override
    public List<BrokerRewardsRecordVo> selectImcBrokerRewardsRecordList(BrokerRewardsRecordForm brokerRewardsRecordForm)
    {
        logger.info("查询代理人奖惩记录列表查询，条件：{}",brokerRewardsRecordForm.toString());
        return imcBrokerRewardsRecordMapper.selectImcBrokerRewardsRecordList(brokerRewardsRecordForm,SecurityUtils.getTenantId());
    }

    /**
     * 新增代理人奖惩记录
     * 
     * @param brokerRewardsRecordForm 代理人奖惩记录
     * @return 结果
     */
    @Override
    public int insertImcBrokerRewardsRecord(BrokerRewardsRecordForm brokerRewardsRecordForm)
    {
        logger.info("新增代理人奖惩记录操作：{}",brokerRewardsRecordForm.toString());
        ImcBrokerRewardsRecord record = new ImcBrokerRewardsRecord();
        BeanUtils.copyProperties(brokerRewardsRecordForm,record);
        record.setCreateTime(DateUtils.getNowDate());
        record.setCreateId(SecurityUtils.getUsername());
        record.setIsDeleted(IsDeletedEnum.N.getCode());
        record.setTenantId(SecurityUtils.getTenantId());
        return imcBrokerRewardsRecordMapper.insert(record);
    }

    /**
     * 修改代理人奖惩记录
     * 
     * @param brokerRewardsRecordForm 代理人奖惩记录
     * @return 结果
     */
    @Override
    public int updateImcBrokerRewardsRecord(BrokerRewardsRecordForm brokerRewardsRecordForm)
    {
        logger.info(" 修改代理人奖惩记录操作：{}",brokerRewardsRecordForm.toString());
        //老数据删除
        ImcBrokerRewardsRecord record = this.imcBrokerRewardsRecordMapper.selectById(brokerRewardsRecordForm.getId());
        record.setIsDeleted(IsDeletedEnum.Y.getCode());
        record.setTenantId(SecurityUtils.getTenantId());
        this.imcBrokerRewardsRecordMapper.updateById(record);

        //最新记录新增
        ImcBrokerRewardsRecord newRecord = new ImcBrokerRewardsRecord();
        BeanUtils.copyProperties(brokerRewardsRecordForm,newRecord);
        newRecord.setId(null);
        newRecord.setUpdateTime(DateUtils.getNowDate());
        newRecord.setUpdateId(SecurityUtils.getUsername());
        newRecord.setCreateTime(DateUtils.getNowDate());
        newRecord.setCreateId(SecurityUtils.getUsername());
        newRecord.setTenantId(SecurityUtils.getTenantId());
        return imcBrokerRewardsRecordMapper.insert(newRecord);
    }

    /**
     * 批量删除代理人奖惩记录
     * 
     * @param ids 需要删除的代理人奖惩记录ID
     * @return 结果
     */
    @Override
    public int deleteImcBrokerRewardsRecordByIds(Long[] ids)
    {
        logger.info("批量删除代理人奖惩记录操作：{}",ids);
        int count = 0;
        for(Long id:ids){
            ImcBrokerRewardsRecord record = this.imcBrokerRewardsRecordMapper.selectById(id);
            record.setIsDeleted(record.getId().toString());
            record.setUpdateId(SecurityUtils.getUsername());
            record.setUpdateTime(DateUtils.getNowDate());
            count += this.imcBrokerRewardsRecordMapper.updateById(record);
        }
        return count;
    }
}
